package net.lzzy.practices.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author  Jaymo on 2022/5/8.
 * Description:
 */
@Entity
@Getter
@Setter
public class Course extends  BaseEntity {

    private  String name;
    private  String description;
    private  String cover;

    @Temporal(TemporalType.TIMESTAMP)
    private  Date  createTime;

    @Temporal(TemporalType.TIMESTAMP)
    private  Date  updateTime;
    private  boolean open;
    @ManyToOne
    private  User teacher;
    @ManyToMany
    private List<User> students = new ArrayList<>();
    @OneToMany(mappedBy = "course")
    private List<Chapter> chapters = new ArrayList<>();
    @OneToMany(mappedBy = "course")
    private  List<Applicant> applicants = new ArrayList<>();

    @JsonManagedReference
    public  List<Applicant>  getApplicants(){
        return  applicants;
    }

    @JsonManagedReference
    public  List<Chapter>    getChapters(){
        return  chapters;
    }

    public  Course(){
        createTime = new Date();
        updateTime = createTime;
    }

    @JsonBackReference
    public User getTeacher() {
        return teacher;
    }

    @JsonIgnore
    public  List<User>  getStudents(){
        return  students;
    }
}
